﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Roboter
{
    class Controller
    {
        private Board board;
        private Robot robot;
        private List<Obstacle> obstacles;

        public Controller(ReadFile input)
        {
            this.board = input.Board;
            this.robot = input.Robot;
            this.obstacles = input.Obstacles;

            //build board

            for (int i = 0; i < board.Width; i++)
            {
                for (int j = 0; j < board.Height; j++)
                {
                    if (i == 0 || i == (board.Width - 1) || j == 0 || j == (board.Height - 1))
                    {
                        board.Fields[i, j] = new Field(FieldState.blocked);
                    }
                    else if (i == robot.PosX && j == robot.PosY)
                    {
                        board.Fields[i, j] = new Field(FieldState.robot);
                    }
                    else
                    {
                        board.Fields[i, j] = new Field(FieldState.free);
                    }
                }
            }

            //add obstacles
            foreach (var item in this.obstacles)
            {
                for (int i = item.X1; i < item.X2 + 1; i++)
                {
                    for (int j = item.Y1; j < item.Y2 + 1; j++)
                    {
                        if (!item.IsExit)
                        {
                            board.Fields[i, j].State = FieldState.blocked;
                        }
                        else
                        {
                            board.Fields[i, j].State = FieldState.free;
                        }
                    }
                }
            }
        }

        public void Draw(Canvas drawArea)
        {
            this.board.Draw(drawArea);
        }
    }
}
     
